﻿#region MIT
/*
Engine Of Evermore (http://code.google.com/p/engineofevermore/)
Copyright (c) 2008, 2009 Michael Woerister

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
#endregion

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Diagnostics;
using System.ComponentModel.Design;

namespace EngineOfEvermore.Editor.SceneEditor.View
{
    partial class MainForm : Form, IMainFormView
    {
        MainFormController _controller;
        SceneToolStripViewAdapter _sceneToolStripViewAdapter;
        UndoRedoButtonsView _undoRedoButtonsView;
        PropertyEditor _propertyEditor;
        ObjectLibrary _objectLibrary;

        bool _suppressSelectionIndexChangedEvent;

        public MainForm()
        {
            this.InitializeComponent();
        
            var cwp = AgateLib.DisplayLib.CreateWindowParams.FromControl( _renderWindowButton );
            
            new AgateLib.DisplayLib.DisplayWindow( cwp );

            _sceneToolStripViewAdapter = new SceneToolStripViewAdapter( _sceneToolStrip );
            _undoRedoButtonsView = new UndoRedoButtonsView(
                new ToolStripItem[] { _toolStripUndoButton, _mainMenuUndoButton },
                new ToolStripItem[] { _toolStripRedoButton, _mainMenuRedoButton } 
                );

            _propertyEditor = new PropertyEditor();
            _propertyEditor.Show( this );

            _objectLibrary = new ObjectLibrary();
            _objectLibrary.Show( this );
        }

        public ISceneToolStripView SceneToolStripView 
        {
            get { return _sceneToolStripViewAdapter; }
        }

        public IDocumentView ActiveDocumentView
        {
            get 
            {
                if ( _documentTabControl.SelectedTab != null )
                {
                    return _documentTabControl.SelectedTab.Controls.OfType<Document>().Single();
                }

                return null;
            }
            set 
            {
                Debug.Assert( value != null );

                if ( this.ActiveDocumentView != null )
                {
                    ( (Document) this.ActiveDocumentView ).IsActiveDocument = false;
                }

                var tabPages = _documentTabControl.TabPages.Cast<TabPage>();
                var page = 
                    tabPages.Single( tb => tb.Controls.OfType<Document>().Single() == value );

                page.Controls.OfType<Document>().Single().IsActiveDocument = true;
                
                _documentTabControl.SelectedTab = page;
            }
        }

        public IUndoRedoButtonsView UndoRedoButtonsView
        {
            get { return _undoRedoButtonsView; }
        }

        public IPropertyEditorView PropertyEditorView
        {
            get { return _propertyEditor; }
        }

        public IObjectLibraryView ObjectLibraryView
        {
            get { return _objectLibrary; }
        }

        public void InitializeController( MainFormController controller )
        {
            _controller = controller;
        }

        public IDocumentView CreateDocumentView( DocumentController controller,
            SceneModel model )
        {
            // This flag is used to handle an unwanted SelectedIndexChanged event
            // that only occurs on Mono
            _suppressSelectionIndexChangedEvent = true;

            TabPage tabPage = new TabPage();
            Document document = new Document( model, tabPage );
            document.Dock = DockStyle.Fill;
            tabPage.Controls.Add( document );
            _documentTabControl.TabPages.Add( tabPage );

            document.Initialize();
            document.InitializeController( controller );

            _suppressSelectionIndexChangedEvent = false;

            return document;
        }

        public void CloseDocumentView( IDocumentView documentView )
        {
            var tabPage = _documentTabControl.TabPages.Cast<TabPage>()
                .Single( page => page.Controls.Contains( (Control) documentView ) );

            // This prevents a crash caused by a bug in Mono's implementation of TabControls
            if ( tabPage == _documentTabControl.SelectedTab )
            {
                if ( _documentTabControl.TabCount > 1 )
                {
                    if ( _documentTabControl.SelectedIndex == 0 )
                    {
                        _documentTabControl.SelectedIndex = 1;
                    }
                    else
                    {
                        _documentTabControl.SelectedIndex -= 1;
                    }
                }
                else
                {
                    _documentTabControl.SelectedTab = null;
                }
            }

            _documentTabControl.TabPages.Remove( tabPage );
            tabPage.Dispose();
        }
        
        private void newToolStripMenuItem_Click( object sender, EventArgs e )
        {
            _controller.NewDocumentClicked();
        }

        private void exitToolStripMenuItem_Click( object sender, EventArgs e )
        {
            this.Close();
        }

        private void toolStripButton1_Click( object sender, EventArgs e )
        {
            _controller.NewDocumentClicked();
        }

        private void openToolStripMenuItem_Click( object sender, EventArgs e )
        {
            _controller.OpenDocumentClicked();
        }

        private void saveToolStripMenuItem_Click( object sender, EventArgs e )
        {
            _controller.SaveDocumentClicked();
        }

        private void toolStripButton1_Click_1( object sender, EventArgs e )
        {
            _controller.SaveDocumentClicked();
        }

        private void _openDocumentToolStripButton_Click( object sender, EventArgs e )
        {
            _controller.OpenDocumentClicked();
        }

        private void _mainMenuItem_CloseDocument_Click( object sender, EventArgs e )
        {
            _controller.CloseDocumentClicked();
        }

        private void _documentTabControl_SelectedIndexChanged( object sender, EventArgs e )
        {
            if ( _suppressSelectionIndexChangedEvent )
                return;

            if ( _documentTabControl.SelectedTab != null )
            {
                var documentView = (IDocumentView) _documentTabControl.SelectedTab.Controls[0];

                _controller.ActiveDocumentViewChanged( documentView );
            }
            else
            {
                _controller.ActiveDocumentViewChanged( null );
            }
        }

        private void undoToolStripMenuItem_Click( object sender, EventArgs e )
        {
            _undoRedoButtonsView.Controller.UndoButtonClicked();
        }

        private void _mainMenuViewPropertyEditor_Click( object sender, EventArgs e )
        {
            _propertyEditor.Show( this );
        }

        private void MainForm_FormClosing( object sender, FormClosingEventArgs e )
        {
            _controller.TryClosing( e );
        }

        private void MainForm_FormClosed( object sender, FormClosedEventArgs e )
        {
            if ( !_objectLibrary.IsDisposed )
            {
                _objectLibrary.Dispose();
            }
        
            if ( !_propertyEditor.IsDisposed )
            {
                _propertyEditor.Dispose();
            }
        }

        private void _mainMenuViewObjectLibrary_Click( object sender, EventArgs e )
        {
            _objectLibrary.Show( this );
        }
    }

    public interface IMainFormView
    {
        ISceneToolStripView SceneToolStripView { get; }
        IDocumentView ActiveDocumentView { get; set; }

        void InitializeController( MainFormController controller );
        IDocumentView CreateDocumentView( DocumentController controller, SceneModel model );

        IUndoRedoButtonsView UndoRedoButtonsView { get; }
        IPropertyEditorView PropertyEditorView { get; }
        IObjectLibraryView ObjectLibraryView { get; }

        void Show();
        void Close();

        void CloseDocumentView( IDocumentView iDocumentView );
    }
}
